--
-- Author: shunguo.chen
-- Date: 2018-12-5 19：54
--
-- RewardFirstPayPanel

local GoddessWishBaseRender = require("app.boss.views.renders.GoddessWishBaseRender")
local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.createWithData(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardFirstPayPanel))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end

function ClassRef.create(node)
	return ClassRef.createWithData(nil, node)
end

--uuid
--awards
--canGet
function ClassRef:init(data)
	self.super.init(self, data)
	--所有奖励的物品
	for i = 1, 3 do
		self._ui["Panel/icon_"..i]:setVisible(false)
	end

	self._ui.Button_buy:addClickEventListener(function()
		--准别跳转到充值界面
		--app:sendMsg("RewardController", "playFirstRechargeGetAwards", self._data.awards)
		app:sendMsg("MallController", "openView", {tabKey = Constant.FUNC_TYPE_RECHARGE})
	end)

	self._ui.Button_lingqu:addClickEventListener(function()
		if self._data then
			executeFunc(self._onOperate, "getFirstRechargeAward", {uuid = self._data.uuid})
		end
	end)
	self._ui.text:setVisible(false)
	self._haveInit = false
end

function ClassRef:refresh()
	if self._data then
		self:setVisible(true)

		if self._data.canGet then
			self._ui.Button_buy:setVisible(false)
			self._ui.Button_lingqu:setVisible(true)
		else
			self._ui.Button_buy:setVisible(true)
			self._ui.Button_lingqu:setVisible(false)
		end

		if self._data.des then
			self._ui.text:setVisible(true)
			self._ui.text:setString(L(self._data.des))
		else
			self._ui.text:setVisible(false)
		end

		--只在初始化的时候做
		if not self._haveInit then
			for k,v in ipairs(self._data.awards) do
				local icon = self._ui["Panel/icon_"..k]
				if icon then
					icon:setVisible(true)
					icon:findChild("bg"):setColor(Enums.QualityColor[v:getQuality()])
					local render = GoddessWishBaseRender.create(icon:findChild("icon"))
					render:setData(v)
					render:setTipEnable(true)
				end
			end
			self._haveInit = true
		end
	else
		self:setVisible(false)
	end
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

return ClassRef
